package com.snatch.gather.util;

import java.io.FileInputStream;
import java.io.StringReader;
import java.io.StringWriter;

import com.snatch.common.vo.LoginAccount;
import com.snatch.common.vo.NodeWeb;
import com.snatch.common.vo.Proxy;
import com.snatch.common.vo.WebConfig;
import com.snatch.common.vo.WebUrl;
import com.snatch.gather.vo.SnatchConfig;
import com.snatch.gather.vo.SnatchGame;
import com.snatch.gather.vo.SnatchItem;
import com.snatch.gather.vo.SnatchWeb;
import com.thoughtworks.xstream.XStream;



public class XmlConfigUtil{
	
	static XStream xs = new XStream();
	
	
	public static NodeWeb xmlChangeNodeWeb(String xmlStr){
		NodeWeb snatchWeb = new NodeWeb();
		StringReader stringReader = new StringReader(xmlStr);
		xs.alias("NodeWeb", NodeWeb.class);
		xs.alias("WebConfig", WebConfig.class);
		xs.alias("WebUrl", WebUrl.class);
		xs.alias("LoginAccount", LoginAccount.class);
		xs.alias("Proxy", Proxy.class);
		xs.fromXML(stringReader, snatchWeb);
		return snatchWeb;
	}
	
	public static NodeWeb xmlChangeNodeWeb(FileInputStream fileInputStream){
		NodeWeb snatchWeb = new NodeWeb();
		xs.alias("NodeWeb", NodeWeb.class);
		xs.alias("WebConfig", WebConfig.class);
		xs.alias("WebUrl", WebUrl.class);
		xs.alias("LoginAccount", LoginAccount.class);
		xs.alias("Proxy", Proxy.class);
		xs.fromXML(fileInputStream, snatchWeb);
		return snatchWeb;
	}
	
	public static String nodeWebChangeXml(NodeWeb snatchWeb){
		xs.alias("NodeWeb", NodeWeb.class);
		xs.alias("WebConfig", WebConfig.class);
		xs.alias("WebUrl", WebUrl.class);
		xs.alias("LoginAccount", LoginAccount.class);
		xs.alias("Proxy", Proxy.class);
		StringWriter sw = new StringWriter();
		xs.toXML(snatchWeb, sw);
		return sw.toString();
	}
	
	
	/*
	 * SNATCH CONFIG
	 */
	
	public static SnatchConfig xmlChangeSnatchConfig(FileInputStream fileInputStream){
		SnatchConfig snatchWeb = new SnatchConfig();
		xs.alias("SnatchConfig", SnatchConfig.class);
		xs.alias("SnatchWeb", SnatchWeb.class);
		xs.alias("SnatchGame", SnatchGame.class);
		xs.alias("SnatchItem", SnatchItem.class);
		xs.fromXML(fileInputStream, snatchWeb);
		return snatchWeb;
	}
	
	public static String nodeWebChangeXml(SnatchConfig snatchWeb){
		xs.alias("SnatchConfig", SnatchConfig.class);
		xs.alias("SnatchWeb", SnatchWeb.class);
		xs.alias("SnatchGame", SnatchGame.class);
		xs.alias("SnatchItem", SnatchItem.class);
		StringWriter sw = new StringWriter();
		xs.toXML(snatchWeb, sw);
		return sw.toString();
	}
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
	}

}
